package org.com.app.knock.fragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.com.app.knock.view.LoadingView;
import org.com.app.knock.view.MessageSItemView;
import org.json.JSONArray;
import org.json.JSONObject;

import z.lib.base.BaseAdapter;
import z.lib.base.BaseItem;
import z.lib.base.BaseServiceCallBack;
import z.lib.base.BaseUtils;
import z.lib.base.BaseUtils.API;
import z.lib.base.BaseView;
import z.lib.base.CommonAndroid;
import z.lib.base.LogUtils;
import z.lib.base.callback.RestClient.RequestMethod;
import android.content.Context;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.acv.libs.nbase.BaseFragment;
import com.app.lazyload.ImageLoader2;
import com.knock.app.R;

public class MSendFragment extends BaseFragment implements AnimationListener {
	private static final String TAG = "UnblockFragment";
	View views;
	ListView listView;
	private BaseAdapter baseAdapter;
	public static boolean isLoguot = false;
	private LoadingView loading;
	public static ImageLoader2 imageLoader;
	public MSendFragment() {
		super();
	}

	@Override
	public void setUpFragment(View view) {
		this.views = view;
		isLoguot = false;
		this.views = view;
		listView =(ListView) views.findViewById(R.id.listview_friend);
		CommonAndroid.getView(view, R.id.btn_back).setOnClickListener(this);		
		loading = CommonAndroid.getView(view, R.id.loading);
		imageLoader = new ImageLoader2(getActivity());
	}
	
	@Override
	public void onResume(){
		super.onResume();
		getBlockFriend();
	}

	private void getBlockFriend() {
		// TODO Auto-generated method stub
		HashMap<String, String> params = new HashMap<String, String>();
		BaseUtils.execute(getActivity(), RequestMethod.POST, API.API_MESSAGE_SEND, params, callback);
	}
	
	private BaseServiceCallBack callback = new BaseServiceCallBack() {

		public void onStart() {
			CommonAndroid.showView(true, loading);
			CommonAndroid.hiddenKeyBoard(getActivity());
		};

		public void onError(String message) {
			CommonAndroid.showView(false, loading);
			if (!isFinish()) {
				LogUtils.e(TAG,"er==" + message);
			}
		};

		public void onSucces(String response) {
			CommonAndroid.showView(false, loading);
			if (!isFinish()) {
				JSONObject mainJsonObject;
				List<BaseItem> baseItems = new ArrayList<BaseItem>();
				baseItems.clear();
				try {
					mainJsonObject = new JSONObject(response);
					JSONArray array = mainJsonObject.getJSONObject("data").getJSONArray("messages");
					for(int i =0; i < array.length();i++){
						if(!"null".equals(array.getJSONObject(i).getString("id"))){
							baseItems.add(new BaseItem(array.getJSONObject(i)));
						}
						
					}
					LogUtils.e(TAG,mainJsonObject.toString());
					loadData(baseItems);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		};
	};

	private void loadData(List<BaseItem> baseItems) {
		// TODO Auto-generated method stub
        baseAdapter = new BaseAdapter(getActivity(), new ArrayList<Object>()) {
			@Override
			public BaseView getView(Context context, Object data) {
				final MessageSItemView phonecodeListItemView = new MessageSItemView(context);
				return phonecodeListItemView;
			}
		};
		baseAdapter.clear();
		baseAdapter.addAllItems(baseItems);
		baseAdapter.notifyDataSetChanged();
		listView.setAdapter(baseAdapter);
	}

	private View views_delete;
	protected void unBlock(View view, String id) {
		// TODO Auto-generated method stub
		views_delete = view;
		HashMap<String, String> params = new HashMap<String, String>();
		params.put("friend_id", id);
		BaseUtils.execute(getActivity(), RequestMethod.POST, API.API_FRIEND_UNBLOCK, params, callbackunblock);
	}

	private BaseServiceCallBack callbackunblock = new BaseServiceCallBack() {

		public void onStart() {
			CommonAndroid.showView(true, loading);
			CommonAndroid.hiddenKeyBoard(getActivity());
		};

		public void onError(String message) {
			CommonAndroid.showView(false, loading);
			if (!isFinish()) {
				LogUtils.e(TAG,"er==" + message);
			}
		};

		public void onSucces(String response) {
			CommonAndroid.showView(false, loading);
			if (!isFinish()) {
				listView.removeView(views_delete);
			}
		};
	};
	
	@Override
	public int layoytResurce() {
		return R.layout.message_s_list;
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_back:
			getActivity().finish();
			break;

		default:
			break;
		}
	}

	@Override
	public void onAnimationStart(Animation animation) {

	}

	@Override
	public void onAnimationEnd(Animation animation) {
		mHandle.sendEmptyMessage(0);
	}

	@Override
	public void onAnimationRepeat(Animation animation) {

	}
}